package com.wan.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.ImageView
import com.google.gson.JsonObject
import com.wan.R
import com.wan.activity.LoginActivity
import com.wan.entity.HomeArtItemEntity
import com.wan.uitls.ApiManager
import com.wan.uitls.CommonUtils
import com.wan.uitls.ItemRightViewListener
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.*

class HomeRvAdapter(context: Context, data: ArrayList<HomeArtItemEntity>, itemViewId: Int) : BaseRecyclerAdapter<HomeArtItemEntity>(context, data, itemViewId) {

    private val mThreadName = this.javaClass.name

    private var rightViewListener:ItemRightViewListener ?=null

    fun setRightViewListener(listener: ItemRightViewListener){
        this.rightViewListener = listener
    }

    override fun convert(holder: BaseViewHolder, itemEntity: HomeArtItemEntity, position: Int) {

        holder.setText(R.id.home_item_title_tv, itemEntity.title)
        holder.setText(R.id.home_item_author_tv, itemEntity.author)
        holder.setText(R.id.home_item_type_tv, "${itemEntity.superChapterName}")
        holder.setText(R.id.home_item_time_tv, itemEntity.niceDate)


        Log.d(mThreadName, "collect : ${itemEntity.collect}")
        holder.setImageResource(R.id.home_item_favorite_iv, if (itemEntity.collect) {
            R.mipmap.ic_collected
        } else {
            R.mipmap.ic_collect
        })


        var imageView = holder.getView<ImageView>(R.id.home_item_favorite_iv)
        imageView.setOnClickListener {
            val state = CommonUtils.getLoginState(context)
            if (state) {
                val collect = itemEntity.collect
                val apiManager = ApiManager(this)
                if (collect) {
                    //取消收藏
                    apiManager.postUnCollectionArticle(itemEntity.id, "")
                            .enqueue(object : Callback<JsonObject> {
                                override fun onFailure(call: Call<JsonObject>, t: Throwable) {

                                    Log.d(mThreadName, "onFailure 取消收藏 : ${call.execute().body()}")

                                }

                                override fun onResponse(call: Call<JsonObject>, response: Response<JsonObject>) {

                                    Log.d(mThreadName, "onResponse 取消收藏 : ${response.body()}")
                                    CommonUtils.toastWord(context, "已取消收藏")
                                    itemEntity.collect = false
                                    data[position] = itemEntity
                                    notifyItemChanged(position,itemEntity)
                                }
                            })
                } else {
                    //收藏
                    apiManager.postCollectionArticle(itemEntity.id, "")
                            .enqueue(object : Callback<JsonObject> {
                                override fun onFailure(call: Call<JsonObject>, t: Throwable) {

                                    Log.d(mThreadName, "onFailure 收藏 : ${call.request().body()}")


                                }

                                override fun onResponse(call: Call<JsonObject>, response: Response<JsonObject>) {

                                    Log.d(mThreadName, "onResponse 收藏 : ${response.body()}")
                                    CommonUtils.toastWord(context, "收藏成功")
                                    itemEntity.collect = true
                                    data[position] = itemEntity
                                    notifyItemChanged(position,itemEntity)

                                }
                            })
                }
                if (rightViewListener != null){
                    rightViewListener!!.onRightViewClickListener(position)
                }
            } else {
                context.startActivity(Intent(context, LoginActivity::class.java))

            }

        }
    }
}


